package com.jbl.st.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.exceptionhandler.ManagerException;
import com.jbl.st.entity.StudentInfo;
import com.jbl.st.service.StudentInfoService;
import com.jbl.st.service.StudentService;
import com.jbl.st.entity.Student;
import com.jbl.st.entity.dto.StudentQuery;
import com.jbl.st.entity.vo.StudentVO;
import com.jbl.te.entity.*;
import com.jbl.te.entity.vo.TeacherVO;
import com.jbl.te.service.DepartmentService;
import com.jbl.te.service.DepartmentclassService;
import com.jbl.te.service.MajorService;
import com.jbl.te.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

import static com.jbl.commonutils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/st/student")
@CrossOrigin
public class StudentController {

    @Autowired
    StudentService studentService;
    @Autowired
    StudentInfoService infoService;
    @Autowired
    DepartmentclassService classService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    MajorService majorService;
    @Autowired
    UserService userService;


    @PostMapping("saveStudentByExcel")
    @ApiOperation(value = "Excel批量导入学生")
    public R saveStudentByExcel(MultipartFile file) {

        studentService.saveStudentByExcel(file,studentService,infoService,departmentService,majorService,classService,userService);
        return R.ok();
    }


    @PostMapping("{page}/{limit}")
    @ApiOperation("分页学生列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) StudentQuery studentQuery) {
        Page<Student> pageParam = new Page<>(page, limit);
        studentService.pageQuery(pageParam,studentQuery);
        List<Student> records = pageParam.getRecords();

        List<StudentVO> list = new ArrayList<>();
        for (Student student : records) {

            StudentVO vo = new StudentVO();
            BeanUtils.copyProperties(student,vo);

            StudentInfo info = infoService.getById(student.getId());
            BeanUtils.copyProperties(info,vo);

            Department department = departmentService.getById(student.getDepartmentId());
            vo.setDepartmentId(department.getId());
            vo.setDepartmentName(department.getName());
            vo.setCollegeId(department.getCollegeId());

            Major major = majorService.getById(student.getMajorId());
            vo.setMajorId(major.getId());
            vo.setMajorName(major.getName());

            Departmentclass departmentclass = classService.getById(student.getDepartmentclassId());
            vo.setDepartmentclassId(departmentclass.getId());
            vo.setDepartmentclassName(departmentclass.getName());

            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }

    @GetMapping("getStudentById")
    @ApiOperation("根据id查找学生")
    public R getStudentById(String id) {
        StudentVO vo = studentService.getStudentById(id);
        return R.ok().data("student",vo);
    }

    @PutMapping("updateStudent")
    @ApiOperation("修改学生")
    public R updateStudent(@RequestBody StudentVO vo) {
        boolean b = studentService.updateStudent(vo);
        if(b) {
            return ok();
        } else {
            return R.error().message("修改失败");
        }
    }

    @DeleteMapping("delStudentById")
    @ApiOperation("删除学生")
    public R delStudentById(String id) {
        Boolean b = studentService.removeStudentById(id);
        if(b) {
            return ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    @DeleteMapping("delStudentsByList")
    @ApiOperation("批量删除学生")
    public R delStudentsByList(@RequestBody  List<String> idList) {
        for (String id : idList) {
            Boolean b = studentService.removeStudentById(id);
            if(!b) return R.error().message("批量删除失败");
        }
        return R.ok();
    }

}

